package com.ruoyi.recycleshop.config;


import org.quartz.Scheduler;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class QuartzConfiguration {
    @Resource
    private JobFactory jobFactory;

    @Bean
    public Scheduler scheduler() {
        return this.getSchedulerFactoryBean().getScheduler();
    }

    @Bean
    public SchedulerFactoryBean getSchedulerFactoryBean() {
        SchedulerFactoryBean sc = new SchedulerFactoryBean();
        sc.setQuartzProperties(this.getProperties());
        sc.setJobFactory(jobFactory);
        return sc;
    }

    /**
     * @description:读取自定义的properties文件
     * @author: jie
     * @time: 2022/1/15 18:24
     */
    public Properties getProperties() {
        try {
            PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
            //设置自定义配置文件的位置
            propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
            //读取配置文件
            propertiesFactoryBean.afterPropertiesSet();
            return propertiesFactoryBean.getObject();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}


